package com.douhulu.bytedance.open.util.json;

import com.douhulu.bytedance.common.util.json.GsonHelper;
import com.douhulu.bytedance.open.bean.ByteOpenCallback;
import com.google.gson.*;

import java.lang.reflect.Type;

/**
 * <h2> 奕泽云 -  </h2>
 *
 * @Author: 奕泽云 - 曾庆雷
 * @Email: 75881401@qq.com
 * @Since: 2022-05-04 13:42
 * CopyRight：山西威斯汀文化传播有限公司
 */
public class ByteOpenCallbackGsonAdapter implements JsonDeserializer<ByteOpenCallback> {
    @Override
    public ByteOpenCallback deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

        ByteOpenCallback callback = new ByteOpenCallback();
        JsonObject jsonObject = jsonElement.getAsJsonObject();
        callback.setLogId(GsonHelper.getString(jsonObject, "log_id"));
        callback.setClientKey(GsonHelper.getString(jsonObject, "client_key"));
        callback.setEventId(GsonHelper.getString(jsonObject, "event_id"));
        callback.setEvent(GsonHelper.getString(jsonObject, "event"));
        callback.setFromUserId(GsonHelper.getString(jsonObject, "from_user_id"));


        switch (GsonHelper.getString(jsonObject, "event")) {
            case "verify_webhook":
                callback.setContent(jsonObject.getAsJsonObject("content").toString());
                break;
            case "authorize":
                JsonArray jsonArray = GsonHelper.getAsJsonArray(jsonObject.getAsJsonObject("content").get("scopes"));
                callback.setContent(jsonArray.toString());
                break;
            case "item_comment_reply":
                callback.setContent(GsonHelper.getString(jsonObject, "content"));
            default:
                break;
        }

        return callback;
    }
}
